package com.zfjt.utils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 把Base64图片转化为二进制流存入相应文件夹中
 * @author xk
 * @version 1.0
 * @date 2017年4月5日
 */
public class WQUtil {
	public static boolean GenerateImage(String imgStr,String filePath)  {   
		//对字节数组字符串进行Base64解码并生成图片  
        if (imgStr == null) //数据为空  
            return false;  
        BASE64Decoder decoder = new BASE64Decoder();  
        try{  
            //Base64解码  
            byte[] b = decoder.decodeBuffer(imgStr);  
            for(int i=0;i<b.length;++i)  {  
                if(b[i]<0)  {//调整异常数据  
                    b[i]+=256;  
                }  
            }  
            System.out.println("-----------------------"+filePath);
            OutputStream out = new FileOutputStream(filePath);      
            out.write(b);  
            out.flush();  
            out.close();   
            return true;  
        }catch (Exception e){  
            return false;  
        }  
    }  
	
	public void checkFile(String filePath){
		
		File file=new File(filePath);    
		if(!file.exists())    
		{  
		    	file.mkdir();    
		}
		
	}
	 public static Map<String, Object> transBean2Map(Object obj) {  
   	  
	        if(obj == null){  
	            return null;  
	        }          
	        Map<String, Object> map = new HashMap<String, Object>();  
	        try {  
	            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());  
	            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();  
	            for (PropertyDescriptor property : propertyDescriptors) {  
	                String key = property.getName();  
	  
	                // 过滤class属性  
	                if (!key.equals("class")) {  
	                    // 得到property对应的getter方法  
	                    Method getter = property.getReadMethod();  
	                    Object value = getter.invoke(obj);  
	  
	                    map.put(key, value);  
	                }  
	  
	            }  
	        } catch (Exception e) {  
	            System.out.println("transBean2Map Error " + e);  
	        }  
	        return map;  
	    } 
	 public static String GetImageStr(String imgFile) {// 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
		 //String imgFile = "C:/Users/谢俅金/Pictures/butterfly.jpg";// 待处理的图片
		 InputStream in = null;
		 byte[] data = null;
		 // 读取图片字节数组
		 try {
		 in = new FileInputStream(imgFile);
		 data = new byte[in.available()];
		 in.read(data);
		 in.close();
		 } catch (IOException e) {
		 e.printStackTrace();
		 }
		 // 对字节数组Base64编码
		 BASE64Encoder encoder = new BASE64Encoder();
		 return encoder.encode(data);// 返回Base64编码过的字节数组字符串
	 }
	 
	 public static void delImage(String filePath){
		 File file = new File(filePath);//文件路径
		 file.delete();
	 }
}
